Skip to content

Conversation

@tzanko-matev
Copy link
Contributor

No description provided.

@tzanko-matev tzanko-matev changed the title Initial python api Design and Initial python api Aug 15, 2025

## Overview

This document outlines the design for integrating Python's `sys.monitoring` API with the `runtime_tracing` format. The goal is to produce CodeTracer-compatible traces for Python programs without modifying the interpreter.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i didn't know much about sys.monitoring before , it sounded a bit like a profile-only api, collecting statistical data, but not every event, but i am probably wrong

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it seems it's indeed a newer alternative to settrace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a new API which can be used for debugging as well

Base automatically changed from push-mrnzmrqxvoxq to main August 18, 2025 10:07
-------- >8 -------- >8 -------- 8< -------- 8< --------
# Everything below the snippet mark will be ignored
#
# Content diff of this revision:
```rs
pub fn on_line(frame: *mut PyFrameObject, lineno: u32);
```
- **INSTRUCTION** – Optionally emit a fine-grained `Event` containing the opcode name for detailed traces.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have Asm events for those cases indeed

```rs
pub fn on_jump(frame: *mut PyFrameObject, target: u32);
```
- **BRANCH** – Record an `Event` with branch outcome (taken or not) to aid coverage analysis.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently this is not part of the trace, but based on AST treesitter analysis matching the taken lines with if and similar; maybe we can support something more fine-grained optionally ? if useful

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll update the spec. There's probably no need to implement this

### C API Boundary
- **C_RETURN** – On returning from a C function, emit a `Return` event tagged as foreign and include result summary.
```rs
pub fn on_c_return(func: *mut PyObject, result: *mut PyObject);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can integrate this with rr backend in the future somehow or with a custom db-backend based C recording(??) but for now seems reasonable

tzanko-matev and others added 3 commits August 19, 2025 14:27
design-docs/design-001.md: Fix design of MonitoringEvents

The values of the events should come from sys.monitoring and not be set in the Rust code, because we risk inconsistencies this way
 
Signed-off-by: Tzanko Matev <[email protected]>

design-docs/design-001.md: Fix event callback types and get rid of PyFrameObject

PyFrameObject is not part of sys.monitroing API

 
Signed-off-by: Tzanko Matev <[email protected]>

design-docs/design-001.md: CodeObject access

Since PyO3 doesn't provide access to PyCodeObject internals we need an alternative way to access them
 
Signed-off-by: Tzanko Matev <[email protected]>

design-docs/design-001.md: We'll ignore BRANCH events initially
 
Signed-off-by: Tzanko Matev <[email protected]>
@tzanko-matev tzanko-matev merged commit a950c1f into main Aug 19, 2025
12 of 13 checks passed
@tzanko-matev tzanko-matev deleted the initial-python-api branch August 19, 2025 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants